///////////////////////////////////////////////////////////////////////////////
//-----------------------------------------------------------------------------
#include <Profiling/ProfilingManager.h>
#ifdef	__MGL_USE_PROFILING_TOOLS__
#include <stdio.h>

//-----------------------------------------------------------------------------

ProfileSample::ProfileSample(std::string name)
{
	this->name = name;
	ProfilingManager::GetInstance()->StartSample( this );
}

//-----------------------------------------------------------------------------

ProfileSample::~ProfileSample()
{
	ProfilingManager::GetInstance()->EndSample( this );
}

//-----------------------------------------------------------------------------

ProfilingManager::ProfilingManager()
{
	m_rootSample = m_lastSample = NULL;
}

//-----------------------------------------------------------------------------

ProfilingManager::~ProfilingManager()
{
	ClearTimeSample();
}

//-----------------------------------------------------------------------------

void		ProfilingManager::StartSample( ProfileSample*	sample )
{
	TimeSample* ts = MGL_NEW TimeSample();
	if( !m_rootSample )
	{
		m_lastSample = m_rootSample = ts;
		m_lastSample->parent = NULL;
	}
	else
	{
		m_lastSample->children.push_back( ts );
		ts->parent = m_lastSample;
		m_lastSample = ts;
	}

	ts->name = sample->name;
	sample->sample = ts;
	sample->timer.start();
	sample->sample->startTime = sample->timer.getStartTimeInMilliSec();
}

//-----------------------------------------------------------------------------

void		ProfilingManager::EndSample( ProfileSample*	sample )
{
	sample->timer.stop();
	sample->sample->diffTime = sample->timer.getElapsedTimeInMilliSec();
	sample->sample->endTime = sample->timer.getEndTimeInMilliSec();
	ASSERT( sample->name == m_lastSample->name );
	m_lastSample = m_lastSample->parent;
}

//-----------------------------------------------------------------------------

void		ProfilingManager::ClearTimeSample()
{
	ClearTimeSample( m_rootSample );
	m_rootSample = m_lastSample = NULL;
}

//-----------------------------------------------------------------------------

void		ProfilingManager::ClearTimeSample( TimeSample *sample )
{
	if( sample )
	{
		std::vector<TimeSample*>::iterator it = sample->children.begin();
		for( ; it != sample->children.end(); it++ )
		{
			ClearTimeSample( *it );
		}
		SAFE_DEL( sample );
	}
}

//-----------------------------------------------------------------------------

void		ProfilingManager::DisplayTimeSample()
{
	PROFILE_OUT( "-----<==== PROFILE SAMPLE ====>-----\n" );
	DisplayTimeSample( m_rootSample, "" );
}

//-----------------------------------------------------------------------------

void		ProfilingManager::DisplayTimeSample( TimeSample* sample, std::string pre_str )
{
	if( sample )
	{
		PROFILE_OUT( "%s%s - %.2fms (%.2fms; %.2fms)\n", pre_str.c_str(), sample->name.c_str(), sample->diffTime, sample->startTime, sample->endTime );
		std::vector<TimeSample*>::iterator it = sample->children.begin();
		for( ; it != sample->children.end(); it++ )
		{
			DisplayTimeSample( *it, pre_str + "  " );
		}
	}
}

//-----------------------------------------------------------------------------
#endif	//	__MGL_USE_PROFILING_TOOLS__
//-----------------------------------------------------------------------------
///////////////////////////////////////////////////////////////////////////////
